/*******************************************************************************
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * Copyright (c) 2011 Michael Chan.
 ******************************************************************************/
package com.google.code.solrdimension.test;

import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.response.FacetField;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.FacetField.Count;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.google.code.solrdimension.core.DimensionFactory;
import com.google.code.solrdimension.core.DimensionResponse;
import com.google.code.solrdimension.core.dimensions.Dimension;
import com.google.code.solrdimension.core.dimensions.DimensionValue;
import com.google.code.solrdimension.test.services.SolrService;


/**
 *Assuming Solr data has been initialized with the example data and solrdimension configured 
 *using the config from the root classpath in the test package
 * 
 * 
 * @author mhc
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:test-application-context.xml" })
public class PerformanceIndicator extends TestCase {
    
    private static Logger logger = LoggerFactory.getLogger(PerformanceIndicator.class);

    @Autowired
    SolrService solrService;

    DimensionFactory factory = DimensionFactory.getInstance();
    
    
    /**
     * reset by {@link PerformanceIndicator#prepEnvironment()} method before every test
     */
    SolrQuery solrQuery;
    
    /**
     * reset by {@link PerformanceIndicator#prepEnvironment()} method before every test
     */
    SolrServer server;

    
    @Before
    public void prepEnvironment() throws Exception{

        solrQuery = new SolrQuery();
        
        server = solrService.getSolrServer();
    }

    /**
     * Dumps raw info about the query
     * @throws Exception
     */
    @Test
    public void performanceTest() throws Exception {

        solrQuery.setQuery("*:*");
        long beforeQuery = System.currentTimeMillis();
        QueryResponse response = server.query(factory.processSolrQuery(solrQuery));
        long afterQuery = System.currentTimeMillis();
        factory.createSolrDimensionResponse(response);
        long afterProcessDimensionResponse = System.currentTimeMillis();
        
        logger.info("Query took " + (afterQuery - beforeQuery) + "ms");
        
        logger.info("Additional processing took: " + (afterProcessDimensionResponse - afterQuery));
        
        
    }
    

}
